//
//  StudyCommentTableViewCell.m
//  Cunli
//
//  Created by 湛亚 on 15/9/25.
//  Copyright (c) 2015年 lilubin. All rights reserved.
//

#import "StudyCommentTableViewCell.h"
#import "PointsManager.h"
#import "UIImageView+WebCache.h"
#import "ConvertToCommonEmoticonsHelper.h"
#import "UIColor+Addition.h"
#import "TTTAttributedLabel.h"
#import "ContentText.h"
#import "CommentModel.h"
#import "YMButton.h"

#define SCREEN_WIDTH ([UIScreen mainScreen].bounds.size.width)

@interface StudyCommentTableViewCell ()<TTTAttributedLabelDelegate>{
    
    TTTAttributedLabel *commentLabel;
    UILabel *countLabel;
}

@end
@implementation StudyCommentTableViewCell

- (id)initWithStyle:(UITableViewCellStyle)style
    reuseIdentifier:(NSString *)reuseIdentifier andContent:(ContentText *)content{
    
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        
        self.backgroundColor=[UIColor convertHexColorToUIColor:0xf6f6f6];
        _headImg=[[UIImageView alloc]init];
        _headImg.frame=CGRectMake(15, 13, 40, 40);
        _headImg.layer.cornerRadius = 20;
        _headImg.clipsToBounds = YES;
        _headImg.userInteractionEnabled=YES;
        if ([PointsManager isDefaultTouXiangWithUrlstring:content.pic]) {
            _headImg.image=[UIImage imageNamed:@"default_touxiang.png"];
        }else{
            [_headImg setImageWithURL:[NSURL URLWithString:content.pic] placeholderImage:[UIImage imageNamed:@"default_touxiang.png"]];
        }
        [self.contentView addSubview:_headImg];
        
        _nameLabel=[[UILabel alloc]initWithFrame:CGRectMake(65, 7, 22*(content.name.length), 30)];
        _nameLabel.text=content.name;
        _nameLabel.userInteractionEnabled=YES;
        _nameLabel.textColor=[UIColor convertHexColorToUIColor:0x999999];
        _nameLabel.font=[UIFont systemFontOfSize:16];
        [self.contentView addSubview:_nameLabel];
        
        if (content.floor) {
            UILabel *floorLab=[[UILabel alloc]initWithFrame:CGRectMake(SCREEN_WIDTH-90, 12, 70, 20)];
            floorLab.text=[NSString stringWithFormat:@"%d楼",content.floor];
            floorLab.textAlignment=2;
            floorLab.textColor=[UIColor convertHexColorToUIColor:0x999999];
            floorLab.font=[UIFont systemFontOfSize:13];
            [self.contentView addSubview:floorLab];
        }
        
        NSString *didReceiveText = [ConvertToCommonEmoticonsHelper
                                    convertToSystemEmoticons:content.content];
        float height=[self heightWithString:didReceiveText];
        commentLabel=[PointsManager createWithframe:CGRectMake(65, 37, SCREEN_WIDTH-80, height) andString:didReceiveText];
        commentLabel.delegate=self;
        [self.contentView addSubview:commentLabel];
        
        height=height+40;
        
        UILabel *timeLab=[[UILabel alloc]initWithFrame:CGRectMake(65, height, 150, 20)];
        timeLab.textColor=[UIColor convertHexColorToUIColor:0x999999];
        timeLab.text=content.addtime;
        timeLab.font=[UIFont systemFontOfSize:13];
        [self.contentView addSubview:timeLab];
        
        _moreButton=[YMButton buttonWithType:UIButtonTypeCustom];
        _moreButton.frame=CGRectMake(SCREEN_WIDTH-50, height-2, 45, 24);
        [_moreButton setImage:[UIImage imageNamed:@"morebuttonimg.png"] forState:0];
        [self.contentView addSubview:_moreButton];
        
        self.selectionStyle=UITableViewCellSelectionStyleNone;
    }
    return self;
}

- (id)initWithStyle:(UITableViewCellStyle)style
    reuseIdentifier:(NSString *)reuseIdentifier andComment:(CommentModel *)content{
    
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        self.backgroundColor=[UIColor convertHexColorToUIColor:0xf6f6f6];
        _headImg=[[UIImageView alloc]init];
        _headImg.frame=CGRectMake(15, 13, 40, 40);
        _headImg.layer.cornerRadius = 20;
        _headImg.clipsToBounds = YES;
        _headImg.userInteractionEnabled=YES;
        if ([PointsManager isDefaultTouXiangWithUrlstring:content.pic]) {
            _headImg.image=[UIImage imageNamed:@"default_touxiang.png"];
        }else{
            [_headImg setImageWithURL:[NSURL URLWithString:content.pic] placeholderImage:[UIImage imageNamed:@"default_touxiang.png"]];
        }
        [self.contentView addSubview:_headImg];
        
        _nameLabel=[[UILabel alloc]initWithFrame:CGRectMake(65, 7, 150, 30)];
        _nameLabel.text=content.name;
        _nameLabel.userInteractionEnabled=YES;
        _nameLabel.textColor=[UIColor convertHexColorToUIColor:0x999999];
        _nameLabel.font=[UIFont systemFontOfSize:16];
        [self.contentView addSubview:_nameLabel];
        
        _praiseButton = [[MaxResponseButton alloc] initWithFrame:CGRectMake(ScreenWidth-35,15, 20, 20)];
        NSString *praiseImgName=content.is_prase==0?@"notpraise":@"addpraise";
        [_praiseButton setImage:[UIImage imageNamed:praiseImgName] forState:UIControlStateNormal];
        [_praiseButton addTarget:self action:@selector(praiseBtnAction) forControlEvents:UIControlEventTouchUpInside];
        [self.contentView addSubview:_praiseButton];
        
        countLabel=[[UILabel alloc]initWithFrame:CGRectMake(ScreenWidth-97,16, 60, 20)];
        countLabel.font=[UIFont systemFontOfSize:14];
        countLabel.textAlignment=2;
        if (content.is_prase==0) {
            countLabel.textColor=[UIColor convertHexColorToUIColor:0x999999];
        }else{
            countLabel.textColor=[UIColor convertHexColorToUIColor:0xfb415c];
        }
        countLabel.text=content.agree>0?[NSString stringWithFormat:@"%ld",content.agree]:@"赞";
        [self.contentView addSubview:countLabel];
        
        //        NSString *didReceiveText = [ConvertToCommonEmoticonsHelper
        //                                    convertToSystemEmoticons:content.content];
        float height=content.commentheight;
        commentLabel=[PointsManager createWithframe:CGRectMake(65, 37, SCREEN_WIDTH-80, height) andString:content.content];
        commentLabel.delegate=self;
        [self.contentView addSubview:commentLabel];
        
        height=height+40;
        UILabel *timeLab=[[UILabel alloc]initWithFrame:CGRectMake(65, height, 150, 20)];
        timeLab.textColor=[UIColor convertHexColorToUIColor:0x999999];
        timeLab.text=content.addtime;
        timeLab.font=[UIFont systemFontOfSize:13];
        [self.contentView addSubview:timeLab];
        
        _moreButton=[YMButton buttonWithType:UIButtonTypeCustom];
        _moreButton.frame=CGRectMake(SCREEN_WIDTH-50,height-4, 45, 24);
        [_moreButton setImage:[UIImage imageNamed:@"morebuttonimg.png"] forState:0];
        [self.contentView addSubview:_moreButton];
        
        self.selectionStyle=UITableViewCellSelectionStyleNone;
    }
    
    return self;
}

-(float)heightWithString:(NSString *)introduce{
    
    CGRect rect = [introduce boundingRectWithSize:CGSizeMake(SCREEN_WIDTH-80, MAXFLOAT) options:NSStringDrawingUsesLineFragmentOrigin attributes:@{NSFontAttributeName:[UIFont systemFontOfSize:17.0f]} context:nil];
    CGFloat height = ceilf(rect.size.height);
    return height+10;
}

- (void)awakeFromNib {
    // Initialization code
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];
    
    // Configure the view for the selected state
}

#pragma mark - TTTAttributedLabelDelegate 点击网址的超链接
- (void)attributedLabel:(TTTAttributedLabel *)label didSelectLinkWithURL:(NSURL *)url {
    
    NSLog(@">>>>>>%@",url);
    NSString *urlStr=[url absoluteString];
    NSRange foundObj=[urlStr rangeOfString:@"http://" options:NSCaseInsensitiveSearch];
    if(foundObj.length>0) {
        [[UIApplication sharedApplication] openURL:url];
    }else{
        
        urlStr=[NSString stringWithFormat:@"http://%@",urlStr];
        [[UIApplication sharedApplication] openURL:[NSURL URLWithString:urlStr]];
    }
    return;
}

-(void)praiseBtnAction{
    
    _praiseClick(_currentRow);
}

-(void)reloadPraiseWithLinLi:(CommentModel *)comment{
    
    if (comment.is_prase==0) {
        [_praiseButton setImage:[UIImage imageNamed:@"notpraise"] forState:UIControlStateNormal];
        countLabel.textColor=[UIColor colorWithHexString:@"0x999999"];
    }else{
        [_praiseButton setImage:[UIImage imageNamed:@"addpraise"] forState:UIControlStateNormal];
        countLabel.textColor=[UIColor colorWithHexString:@"0xfb415c"];
    }
    countLabel.text=comment.agree>0?[NSString stringWithFormat:@"%ld",comment.agree]:@"赞";
}

@end
